﻿using ShellProgressBar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ImageSkiaSharp
{
    internal class Progress
    {
        // 请求退出执行
        public static bool RequestToQuit { get; set; }
        public static ProgressBarOptions Options= new ProgressBarOptions
        {
            // 设置前景色
            ForegroundColor = ConsoleColor.Yellow,
            // 设置完成时的前景色
            ForegroundColorDone = ConsoleColor.DarkGreen,
            // 设置背景色
            BackgroundColor = ConsoleColor.DarkGray,
            // 设置背景字符
            BackgroundCharacter = '\u2593'
        };
        // 标记完成方法
        public static void TickToCompletion(IProgressBar pbar, int ticks, int sleep = 1750, Action<int> childAction = null)
        {
            var initialMessage = pbar.Message;
            for (var i = 0; i < ticks && !RequestToQuit; i++)
            {
                pbar.Message = $"Start {i + 1} of {ticks} {Console.CursorTop}/{Console.WindowHeight}: {initialMessage}";
                childAction?.Invoke(i);
                Thread.Sleep(sleep);
                pbar.Tick($"End {i + 1} of {ticks} {Console.CursorTop}/{Console.WindowHeight}: {initialMessage}");
            }
        }

        public static void Start(int total,string msg)
        {
            int totalTicks = total;
            var options = new ProgressBarOptions
            {
                // 设置前景色
                ForegroundColor = ConsoleColor.Yellow,
                // 设置完成时的前景色
                ForegroundColorDone = ConsoleColor.DarkGreen,
                // 设置背景色
                BackgroundColor = ConsoleColor.DarkGray,
                // 设置背景字符
                BackgroundCharacter = '\u2593'
            };
            using (var pbar = new ProgressBar(totalTicks, msg, options))
            {
                TickToCompletion(pbar, totalTicks, sleep: 500);
            }
        }
    }
}
